package Controller;

import entity.Admin;
import entity.Borrow;
import service.BookService;
import service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/admin")
public class AdminServlet extends HttpServlet {
    private BookService bookService=new BookServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        HttpSession session=req.getSession();
        Admin admin = (Admin)session.getAttribute("admin");
        if(method == null){
            method="findAllBorrow";
        }
        switch (method){
            case "findAllBorrow":
                /*List<Borrow> borrowList= null;*/
                try {
                    String pagestr = req.getParameter("page");
                    Integer page=Integer.parseInt(pagestr);
                    List<Borrow> borrowList = bookService.findAllBorrowByState(0,page);
                    req.setAttribute("list",borrowList);
                    req.setAttribute("dataPrepare",6);
                    req.setAttribute("currentPage",page);
                    req.setAttribute("pages",bookService.getBorrowPagesByState(0));
                    req.getRequestDispatcher("admin.jsp").forward(req,resp);
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case "handle":
                String idstr = req.getParameter("id");
                String statestr = req.getParameter("state");
                Integer id = Integer.parseInt(idstr);
                Integer state = Integer.parseInt(statestr);
                try {
                    bookService.handleBorrow(id,state,admin.getId());
                    if(state ==1 || state==2){
                        resp.sendRedirect("/admin?page=1");
                    }
                    if(state==3){
                        resp.sendRedirect("/admin?method=getBorrowed&page=1");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                break;
            case"getBorrowed":
                String pagestr = req.getParameter("page");
                Integer page=Integer.parseInt(pagestr);
                List<Borrow> borrowList = null;
                try {
                    borrowList = bookService.findAllBorrowByState(1,page);
                    req.setAttribute("list",borrowList);
                    req.setAttribute("dataPrepare",6);
                    req.setAttribute("currentPage",page);
                    req.setAttribute("pages",bookService.getBorrowPagesByState(1));
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                req.getRequestDispatcher("return.jsp").forward(req,resp);


        }
    }
}
